﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Sunny.Entity;
using Sunny.Model.RequestParam;
using Sunny.Service;
using Sunny.Util.Attributes;
using Sunny.Util.Enums;

namespace Sunny.Api.Controllers
{
    [ApiController]
    [Route("system/post")]
    public class SysPostController
    {
        private readonly PostService _postService;

        public SysPostController(PostService postService)
        {
            _postService = postService;
        }

        #region 岗位列表
        [HasPermission("system:post:list")]
        [Route("list")]
        public async Task<Result> List([FromQuery]PostListParam param)
        {
            (List<SysPost> sysPosts,int total) = await _postService.List(param);
            return Result.Success(new
            {
                rows = sysPosts,
                total
            });
        }

        #endregion

        #region 新增岗位
        [HasPermission("system:post:add")]
        [LogInfo(Title = "岗位管理",BusinessType = BusinessType.Insert)]
        [HttpPost]
        public async Task<Result> Add([FromBody]SysPost sysPost)
        {
            await _postService.Add(sysPost);
            return Result.Success();
        }

        #endregion

        #region 获取岗位信息
        [HasPermission("system:post:query")]
        [HttpGet("{postId}")]
        public async Task<Result> Info([FromRoute]long postId)
        {
            SysPost sysPost = await _postService.GetPostById(postId);
            return Result.Success(sysPost);
        }
        #endregion
        
        #region 修改岗位
        [HasPermission("system:post:edit")]
        [LogInfo(Title = "岗位管理",BusinessType = BusinessType.Update)]
        [HttpPut]
        public async Task<Result> Edit([FromBody]SysPost sysPost)
        {
            await _postService.Update(sysPost);
            return Result.Success();
        }

        #endregion

        #region 删除岗位
        [HasPermission("system:post:remove")]
        [LogInfo(Title = "岗位管理",BusinessType = BusinessType.Delete)]
        [HttpDelete("{postIds}")]
        public async Task<Result> Delete([FromRoute]string postIds)
        {
            await _postService.Delete(postIds);
            return Result.Success();
        }
        #endregion
    }
}